// A clickable button which executes a function passed as an argument

import nme.display.MovieClip;
import nme.events.MouseEvent;
import nme.text.TextField;
import nme.text.TextFormat;

class Button extends MovieClip {
    public var home:MovieClip;
    public var action:Void->Void;
    public var span:Float;
    public var depth:Float;
    public var calc:Calc;
    public var txt:TextField;

    public function new(myHome:MovieClip, myX:Float, myY:Float, myWidth:Float, myHeight:Float, myFill:Int, myOutline:Int, myText:String, myAction:Void -> Void) {
        super();

        // Determine the base class
        home = myHome;

        // Set and prepare the action
        action = myAction;
        addEventListener(MouseEvent.MOUSE_DOWN, takeAction);

        // Define the perimeter
        span = myWidth;
        depth = myHeight;

        // Create a calc, containing general use functions
        calc = new Calc(this);

        // Position the object
        x = myX;
        y = myY;
        
        // Draw the rectangle
        graphics.beginFill(myFill);
        graphics.lineStyle(3, myOutline);
        graphics.drawRect(0, 0, span, depth);

        // Write the label
        txt = new TextField();
        txt.text = myText;
        txt.selectable = false;
        txt.x = calc.perXAbs(.5) - txt.textWidth / 2;
        txt.y = calc.perYAbs(.5) - txt.textHeight / 2;
        addChild(txt);

        home.addChild(this);
    } // public function new(grid:MovieClip, columnNumber:Int, rowNumber:Int)

    public function takeAction(event:MouseEvent) { action(); }

} // class Button extends MovieClip
